Polish English
Runaurufu "If you don't hava a plan - do not follow plan of others"

open_basedir restriction in effect

If you ever received error similar to this one:

Warning: session_start() [function.session-start]: open_basedir restriction in effect. File(/var/tmp/) is not within the allowed path(s): (/tmp:/home:/usr/local/ispmgr/distfiles) in /home/user/data/www/somefile.php on line

Fatal error: session_start() [function.session-start]: Failed to initialize storage module: files (path: ) in /home/user/data/www/somefile.php on line
then it is a sign that your hosting is making troubles where it shouldn't.
Browsing web in hope for quick and easy solution most likely will lead you to many ways to solve it... but to do so you most likely would need full access to server configuration what is nearly impossible if you are using shared hosting service (like ultimahost.pl).
Thankfully PHP gives programmer huge liberty in overcoming server limitations.

Issue origin?

The source of this issue in most cases is variable session.save_path value set to directory which we (user interpreting PHP code) cannot access and/or modify. If this variable value is not set PHP will use default path - "/var/tmp/", which also may point to location which user cannot fully access (what is most common problem for shared hosting services).
This directory is a place where state of all active sessions are stored, so code is unable to utilize sessions as long this location do not allow us to create and modify files.

Simple solution

Now when we know that problem lies in incorrect path used to save session files, all we need to do is:
  1. Create directory on server in which we will store session data.
  2. Execute in code before first call to session_start() (or even at the very beginning of your PHP bootstrap just after first <?php) following code:
    session_save_path($sessionDirectoryLocation);
    in order to use folder "sessions" in same directory where calling script is located you can call:
    session_save_path($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'sessions');

    An alternative is to execute function ini_set():
    ini_set('session.save_path', $sessionDirectoryLocation);
    which does exactly the same what session_save_path() - it sets configuration variable session.save_path.
2014-02-11 19:16:41
2014-02-20 20:12:05

Tags: PHP
Categories: IT related

No comments

Write new comment

Author:
Email (visible only to administration):
Website: